package com.wsoft.portal.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.exception.Asserts;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.portal.entity.PortalOrgMemberEntity;
import com.wsoft.portal.entity.PortalThemeEntity;
import com.wsoft.portal.enums.RenderTypeEnum;
import com.wsoft.portal.mapper.PortalOrgMemberMapper;
import com.wsoft.portal.query.org.member.OrgMemberListQuery;
import com.wsoft.portal.vo.org.member.OrgMemberVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author z001
 * @Date 2024/12/27 10:09
 */
@Service
public class PortalOrgMemberManager extends ServiceImpl<PortalOrgMemberMapper, PortalOrgMemberEntity> {


    @Resource
    private PortalThemeManager themeManager;
    @Resource
    private RedisService redisService;

    /**
     * 根据主题id查询 ==> 查询用户或者组织下的数据
     * @param themeId
     * @param orgId
     * @param memberId
     * @return
     */
    public PortalOrgMemberEntity getByThemeId(Long themeId,Long orgId,Long memberId){
        if(null == orgId && null == memberId){
            //组织和用户不能同时为空
            Asserts.fail("[portal.org.member.not.null]");
        }
        QueryWrapper<PortalOrgMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalOrgMemberEntity::getThemeId,themeId)
                .eq(null != orgId,PortalOrgMemberEntity::getOrgId,orgId)
                .eq(null != memberId,PortalOrgMemberEntity::getMemberId,memberId);
        return getOne(query);
    }

    /**
     * 获取组织和用户都为空的默认数据
     * @param themeId
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Portal.ORG_MEMBER_THEME_ID,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public PortalOrgMemberEntity getThemeDefault(Long themeId){
        QueryWrapper<PortalOrgMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalOrgMemberEntity::getThemeId,themeId)
                .isNull(PortalOrgMemberEntity::getOrgId)
                .isNull(PortalOrgMemberEntity::getMemberId);
        return getOne(query);
    }

    @GetRedisCache(type = RedisKeyConstant.Portal.ORG_MEMBER_INFO,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public PortalOrgMemberEntity getById(Long id){
        return super.getById(id);
    }
    /**
     * 根据id获取
     * @param id
     * @return
     */
    public OrgMemberVO getOrgMemberInfo(Long id){
        PortalOrgMemberEntity entity = this.getById(id);
        OrgMemberVO memberVO = Convert.convert(OrgMemberVO.class, entity);
        PortalThemeEntity themeEntity = themeManager.getById(entity.getThemeId());
        if(null != themeEntity){
            memberVO.setThemeName(themeEntity.getName());
        }
        return memberVO;
    }


    /**
     * 根据id修改
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(PortalOrgMemberEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.Portal.ORG_MEMBER_INFO + entity.getId());
        }
        return b;
    }

    /**
     * 移除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        boolean b = super.removeById(id);
        if(b){
            redisService.del(RedisKeyConstant.Portal.ORG_MEMBER_INFO +id);
        }
        return b;
    }

    /**
     * 根据 当前主题id 删除
     * @param themeId
     * @param orgId
     * @param memberId
     * @return
     */
    public boolean removeByThemeId(Long themeId,Long orgId,Long memberId){
        PortalOrgMemberEntity memberEntity = getByThemeId(themeId, orgId, memberId);
        if(null == memberEntity){
            return true;
        }
        return removeById(memberEntity.getId());
    }

    /**
     * 根据 当前主题id 删除
     * @param themeId
     * @param orgId
     * @param memberId
     * @return
     */
    public boolean removeByThemeId(Long themeId){
        QueryWrapper<PortalOrgMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalOrgMemberEntity::getThemeId,themeId);
        boolean b = remove(query);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.PATTERN_SYMBOL);

        }
        return b;
    }

    /**
     * 分页查询
     * @param param
     * @return
     */
    public PageInfo  pageOrgMember(OrgMemberListQuery param) {
        QueryWrapper<PortalOrgMemberEntity> query = new QueryWrapper<>();

        if(StrUtil.isNotBlank(param.getThemeCode()) || StrUtil.isNotBlank(param.getThemeName())){
            QueryWrapper<PortalThemeEntity> themeQuery = new QueryWrapper<>();
            themeQuery.lambda().like(StrUtil.isNotBlank(param.getThemeCode()),PortalThemeEntity::getCode,param.getThemeCode())
                            .like(StrUtil.isNotBlank(param.getThemeName()),PortalThemeEntity::getName,param.getThemeName());
            List<PortalThemeEntity> list = themeManager.list(themeQuery);
            if(CollUtil.isNotEmpty(list)){
                List<Long> idList = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
                query.lambda().in(PortalOrgMemberEntity::getThemeId, idList);
            }else{
                return PageInfo.of(new ArrayList<>());
            }
        }
        if(RenderTypeEnum.ORG.getType().equals(param.getType())){
            query.lambda().isNotNull(PortalOrgMemberEntity::getOrgId)
                    .eq(null != param.getOrgId(),PortalOrgMemberEntity::getOrgId,param.getOrgId());
        }else{
            query.lambda().isNotNull(PortalOrgMemberEntity::getMemberId)
                    .eq(null != param.getMemberId(),PortalOrgMemberEntity::getMemberId,param.getMemberId());
        }
        query.lambda().orderByAsc(PortalOrgMemberEntity::getSort);
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<PortalOrgMemberEntity> list = list(query);
        PageInfo pageInfo = PageInfo.of(list);
        List<OrgMemberVO> result = new ArrayList<>();
        for (PortalOrgMemberEntity entity : list) {
            OrgMemberVO memberVO = Convert.convert(OrgMemberVO.class, entity);
            PortalThemeEntity themeEntity = themeManager.getById(entity.getThemeId());
            if(null != themeEntity){
                memberVO.setThemeName(themeEntity.getName());
                memberVO.setThemeCode(themeEntity.getCode());
            }
            result.add(memberVO);
        }
        pageInfo.setList(result);
        return pageInfo;
    }
}
